<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>等比数列生成器</title>
    <link rel="stylesheet"
        href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/styles/atom-one-dark.min.css">
    <link rel="stylesheet" href="../../demo/plugins/input-number/style.css">
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            margin: 0;
            background-color: #f0f2f5;
            color: #333;
            line-height: 1.6;
        }

        .ew-input-number {
            width: 100%;
        }

        .container {
            background-color: #ffffff;
            padding: 40px;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 700px;
            box-sizing: border-box;
            border: 1px solid #e0e0e0;
            margin-top: 30px;
            margin-bottom: 30px;
        }

        h1 {
            text-align: center;
            color: #2c3e50;
            margin-bottom: 30px;
            font-size: 2.5em;
            text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.05);
        }

        h2 {
            margin-top: 25px;
            margin-bottom: 15px;
            border-bottom: 2px solid #eee;
            padding-bottom: 10px;
        }

        .input-group {
            margin-bottom: 20px;
        }

        .input-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: bold;
            color: #555;
        }

        .button-group {
            display: flex;
            justify-content: center;
            gap: 15px;
            margin-top: 30px;
        }

        .button-group button {
            padding: 12px 25px;
            border: none;
            border-radius: 8px;
            font-size: 1.1em;
            cursor: pointer;
            transition: background-color 0.3s ease, transform 0.2s ease;
            color: #fff;
            font-weight: bold;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
        }

        .button-group button.generate {
            background-color: #28a745;
        }

        .button-group button.generate:hover {
            background-color: #218838;
            transform: translateY(-2px);
        }

        .button-group button.reset {
            background-color: #6c757d;
        }

        .button-group button.reset:hover {
            background-color: #5a6268;
            transform: translateY(-2px);
        }

        .result-area {
            background-color: #e9ecef;
            padding: 10px;
            border-radius: 8px;
            min-height: 100px;
            white-space: pre-wrap;
            word-wrap: break-word;
            font-family: 'Consolas', 'Monaco', monospace;
            font-size: 0.95em;
            color: #333;
            border: 1px solid #dee2e6;
            overflow-x: auto;
        }

        .error-message {
            color: #dc3545;
            background-color: #f8d7da;
            border: 1px solid #f5c6cb;
            padding: 10px;
            border-radius: 8px;
            margin-top: 15px;
            display: none;
        }

        .code-snippet {
            margin-top: 30px;
            background-color: #2d2d2d;
            color: #f8f8f2;
            padding: 20px;
            border-radius: 8px;
            font-family: 'Fira Code', 'Consolas', 'Monaco', monospace;
            font-size: 0.9em;
            overflow-x: auto;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
        }

        .code-snippet pre {
            margin: 0;
        }
    </style>
</head>

<body>
    <h1>等比数列生成器</h1>

    <div class="container">
        <h2>输入参数</h2>
        <div class="input-group">
            <label for="endValue">结束值 (End):</label>
            <div id="endValue" name="endValue"></div>
        </div>

        <div class="input-group">
            <label for="startValue">起始值 (Start, 默认为 1):</label>
            <div id="startValue" name="startValue"></div>
        </div>

        <div class="input-group">
            <label for="stepValue">步长 (Step, 默认为 2):</label>
            <div id="stepValue" name="stepValue"></div>
        </div>

        <div class="button-group">
            <button class="generate" onclick="generateProgression()">生成数列</button>
            <button class="reset" onclick="resetForm()">重置</button>
        </div>

        <h2>生成结果</h2>
        <div class="result-area" id="resultArea"></div>
        <div class="error-message" id="errorMessage"></div>
    </div>

    <div class="code-snippet">
        <h2>核心代码片段: geometricProgression 函数</h2>
        <pre><code class="language-javascript">
const geometricProgression = (end, start = 1, step = 2) =>
    Array.from({
        length: Math.floor(Math.log(end / start) / Math.log(step)) + 1
    }).map((_, i) => start * step ** i);
        </code></pre>
    </div>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/highlight.min.js"></script>
    <script src="../../demo/plugins/input-number/index.js"></script>
    <script>
        hljs.highlightAll();

        const endValueInstance = new InputNumber({
            value: 256,
            container: "#endValue"
        });
        const startValueInstance = new InputNumber({
            value: 1,
            container: "#startValue"
        });
        const stepValueInstance = new InputNumber({
            value: 2,
            container: "#stepValue"
        });

        const geometricProgression = (end, start = 1, step = 2) =>
            Array.from({
                length: Math.floor(Math.log(end / start) / Math.log(step)) + 1
            }).map((_, i) => start * step ** i);

        function showError(message) {
            const errorDiv = document.getElementById('errorMessage');
            errorDiv.textContent = message;
            errorDiv.style.display = 'block';
        }

        function clearError() {
            document.getElementById('errorMessage').style.display = 'none';
        }

        function generateProgression() {
            clearError();
            const endValue = parseFloat(endValueInstance.getValue());
            const startValue = parseFloat(startValueInstance.getValue());
            const stepValue = parseFloat(stepValueInstance.getValue());
            const resultArea = document.getElementById('resultArea');
            resultArea.innerHTML = '';

            if (isNaN(endValue) || isNaN(startValue) || isNaN(stepValue)) {
                showError('请输入有效的数字！');
                return;
            }

            if (startValue === 0) {
                showError('起始值不能为 0！');
                return;
            }

            if (stepValue <= 0) {
                showError('步长必须大于 0！');
                return;
            }

            if (endValue < startValue && stepValue > 1) {
                showError('当步长大于1时，结束值不能小于起始值！');
                return;
            }

            if (endValue > startValue && stepValue < 1 && stepValue > 0) {
                showError('当步长小于1且大于0时，结束值不能大于起始值！');
                return;
            }

            if (endValue !== startValue && stepValue === 1) {
                showError('当步长为1时，结束值必须等于起始值！');
                return;
            }

            try {
                const result = geometricProgression(endValue, startValue, stepValue);
                resultArea.innerHTML = `
                    <pre><code class="language-javascript">${JSON.stringify(result, null, 2)}</code></pre>
                `;

                hljs.highlightElement(resultArea.querySelector('code'));
            } catch (e) {
                showError('生成数列时发生错误: ' + e.message);
                console.error(e);
            }
        }

        function resetForm() {
            document.getElementById('endValue').value = '256';
            document.getElementById('startValue').value = '1';
            document.getElementById('stepValue').value = '2';
            document.getElementById('resultArea').textContent = '';
            clearError();
        }
    </script>
</body>

</html>